#include <dbg.h>

#include <cassert>
#include <set>

using namespace std;

class Solution {
public:
    long long continuousSubarrays(vector<int>& nums) {
        long long res = 0;
        multiset<int> s;
        int left = 0;
        for (int right = 0; right < nums.size(); ++right) {
            s.insert(nums[right]);
            while (*s.rbegin() - *s.begin() > 2) {
                s.erase(s.find(nums[left]));
                ++left;
            }
            res += (right - left + 1);
        }
        return res;
    }
};

int main() {
    Solution s;

    vector<int> nums1{5, 4, 2, 4};
    vector<int> nums2{1, 2, 3};

    assert(s.continuousSubarrays(nums1) == 8);
    assert(s.continuousSubarrays(nums2) == 6);

    return 0;
}